package com.cardbaobao.cardbabyclient.widget.refresh;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;

/**
 * Created by Jiangzx on 2017/7/25 10:43.
 */

public class HeaderRefresh extends LinearLayout implements RefreshHeader {
    private static final String STR_PULL_TO_REFRESH = "下拉刷新";
    private static final String STR_RELEASE_TO_REFRESH = "松开开始刷新";
    private static final String STR_REFRESHING = "正在刷新...";
    private static final String STR_REFRESH_COMPLETE="刷新完成";
    private ImageView refreshArrow;
    private TextView refreshTextView;
    public HeaderRefresh(@NonNull Context context) {
        this(context,null);
    }

    public HeaderRefresh(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public HeaderRefresh(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.widget_header_refresh,this,true);
        refreshArrow = (ImageView)findViewById(R.id.iv_arrow);
        refreshTextView = (TextView)findViewById(R.id.tv);
    }

    @Override
    public void onPullingDown(float percent, int offset, int headerHeight, int extendHeight) {

    }

    @Override
    public void onReleasing(float percent, int offset, int headerHeight, int extendHeight) {

    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {

    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        return getResources().getInteger(R.integer.refreshDelay);
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        switch (newState) {
            case None:
            case PullDownToRefresh:
                refreshArrow.setImageResource(R.mipmap.icon_pull_long);
                refreshArrow.setRotation(0);
                refreshTextView.setText(STR_PULL_TO_REFRESH);
                break;
            case ReleaseToRefresh:
                refreshArrow.setImageResource(R.mipmap.icon_pull_long);
                refreshArrow.setRotation(180);
                refreshTextView.setText(STR_RELEASE_TO_REFRESH);
                break;
            case Refreshing:
                refreshArrow.setImageResource(R.drawable.anim_loading_view);
                ((AnimationDrawable)refreshArrow.getDrawable()).start();
                refreshTextView.setText(STR_REFRESHING);
                break;
            case RefreshFinish:
                refreshArrow.setImageResource(R.drawable.anim_loading_view);
                ((AnimationDrawable)refreshArrow.getDrawable()).stop();
                refreshTextView.setText(STR_REFRESH_COMPLETE);
                break;
        }
    }
}
